package com.ie.service.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ie.common.http.ResponseResult;
import com.ie.entity.CartEntity;
import com.ie.entity.GoodsEntity;
import com.ie.entity.OrderdetailEntity;
import com.ie.entity.OrdersEntity;
import com.ie.mapper.admin.OrdersMapper;
import com.ie.service.before.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, OrdersEntity> implements OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private OrderdetailService orderdetailService;
    @Autowired
    private CartService cartService;

    @Override
    public ResponseResult<Map<String, Object>> getAllOrders(OrdersEntity ordersEntity) {
        IPage<OrdersEntity> iPage = new Page<>(ordersEntity.getCurrentPage(), 5);
        //构造条件
        QueryWrapper<OrdersEntity> queryWrapper = new QueryWrapper<>();
        if (ordersEntity.getId() != null && ordersEntity.getId() > 0)
            queryWrapper.eq("id", ordersEntity.getId());
        //分页查询（商品管理进来时初始查询及条件查询）
        IPage<OrdersEntity> page = page(iPage, queryWrapper);
        Map<String, Object> myres = new HashMap<>();
        myres.put("allOrders", page.getRecords());
        myres.put("totalPage", page.getPages());
        return ResponseResult.getSuccessResult(myres);
    }

    @Override
    public ResponseResult<List<Map<String, Object>>> getOrdersDetail(OrdersEntity ordersEntity) {
        return ResponseResult.getSuccessResult(ordersMapper.getOrdersDetail(ordersEntity));
    }

    @Override
    public ResponseResult<OrdersEntity> submitOrder(OrdersEntity ordersEntity) {
        ordersEntity.setStatus(0);
        ordersEntity.setOrderdate(new Date());
        //生成订单
        save(ordersEntity);
        //生成订单详情
        List<Integer> bgid = ordersEntity.getBgid();
        List<Integer> bshoppingnum = ordersEntity.getBshoppingnum();
        List<OrderdetailEntity> bods = new ArrayList<OrderdetailEntity>();
        List<GoodsEntity> ges = new ArrayList<GoodsEntity>();
        for (int i = 0; i < bgid.size(); i++) {
            OrderdetailEntity ot = new OrderdetailEntity();
            GoodsEntity ge = new GoodsEntity();
            ot.setOrderbasetableId(ordersEntity.getId());
            ot.setGoodstableId(bgid.get(i));
            ot.setShoppingnum(bshoppingnum.get(i));
            bods.add(ot);
            ge.setId(bgid.get(i));
            //减少库存
            ge.setGstore(goodsService.getById(bgid.get(i)).getGstore() - bshoppingnum.get(i));
            ges.add(ge);
        }
        orderdetailService.saveBatch(bods);
        //批量更新库存
        goodsService.updateBatchById(ges);
        //清空购物车
        CartEntity cartEntity = new CartEntity();
        cartEntity.setBusertableId(ordersEntity.getBusertableId());
        cartService.clearCart(cartEntity);
        return ResponseResult.getSuccessResult(ordersEntity);
    }

    @Override
    public ResponseResult<OrdersEntity> goSubmitOrder(OrdersEntity ordersEntity) {
        ordersEntity.setStatus(0);
        ordersEntity.setOrderdate(new Date());
        //生成订单
        save(ordersEntity);
        //生成订单详情
        List<Integer> bgid = ordersEntity.getBgid();
        List<Integer> bshoppingnum = ordersEntity.getBshoppingnum();
        List<OrderdetailEntity> bods = new ArrayList<OrderdetailEntity>();
        List<GoodsEntity> ges = new ArrayList<GoodsEntity>();
        for (int i = 0; i < bgid.size(); i++) {
            OrderdetailEntity ot = new OrderdetailEntity();
            GoodsEntity ge = new GoodsEntity();
            ot.setOrderbasetableId(ordersEntity.getId());
            ot.setGoodstableId(bgid.get(i));
            ot.setShoppingnum(bshoppingnum.get(i));
            bods.add(ot);
            ge.setId(bgid.get(i));
            //减少库存
            ge.setGstore(goodsService.getById(bgid.get(i)).getGstore() - bshoppingnum.get(i));
            ges.add(ge);
        }
        orderdetailService.saveBatch(bods);
        //批量更新库存
        goodsService.updateBatchById(ges);
        return ResponseResult.getSuccessResult(ordersEntity);
    }

    @Override
    public ResponseResult<Map<String, Object>> getOrdersByUid(OrdersEntity ordersEntity) {
        IPage<OrdersEntity> ipage = new Page<>(ordersEntity.getCurrentPage(), 5);
        //构造条件
        QueryWrapper<OrdersEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("busertable_id", ordersEntity.getBusertableId());
        IPage<OrdersEntity> page = page(ipage, queryWrapper);
        HashMap<String, Object> map = new HashMap<>();
        map.put("ordersByUid", page.getRecords());
        map.put("totalPage", page.getTotal());
        return ResponseResult.getSuccessResult(map);
    }
}
